import aliyunApi from '@/common/api/aliyun-api';
import { Minio } from 'minio-js';
let tokenCache = new Map();

export function getOssToken(command) {
    let key = JSON.stringify(command);

    if (tokenCache.has(key) && tokenCache.get(key).expiration > Date.now()) {
        return Promise.resolve(tokenCache.get(key));
    }

    return aliyunApi.requestOssToken(command).then((response) => {
        let token = response.data;
        tokenCache.set(key, token);
        return token;
    });
}
function fetchData() {
    return Promise.resolve("执行链式方法");

  }
export function createOssClient(ossToken) {
    return new Minio.Client({
        endPoint: "47.108.157.172",
        port: 9100,
        useSSL: false,
        accessKey: "g1tIcuEQ7FGznrl2",
        secretKey: "wwVdN2wTV99qyjZRhdsNTGg3GFm3uJNQ",
        }); 
    return fetchData().then(({default: _}) => {
        return new Minio.Client({
            endPoint: "47.108.157.172",
            port:"9100",
            useSSL: true,
            accessKey: ossToken.accessKeyId,
            secretKey: ossToken.accessKeySecret,
            });   
        // return new _({
        //     endpoint: ossToken.endpoint,
        //     secure: ossToken.secure,
        //     accessKeyId: ossToken.accessKeyId,
        //     accessKeySecret: ossToken.accessKeySecret,
        //     // accessKeyId: "g1tIcuEQ7FGznrl2",
        //     // accessKeySecret: "wwVdN2wTV99qyjZRhdsNTGg3GFm3uJNQ",
        //     stsToken: ossToken.securityToken,
        //     bucket: ossToken.bucket,
        //     refreshSTSTokenInterval: 60000 * 5,//去掉warning，当前没用，因为需要配合refreshSTSToken使用
        //     cname: true
        // });
    });
}